package communication;

import java.io.*;
import java.net.*;

import exception.ParseException;

public class ServerConnectionHandle {

	private String ip;
	private int port;
	private Socket connToServer;

	ConnectionHandle hndConnection;

	public ServerConnectionHandle(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}

	protected void connect() throws IOException {
		connToServer = new Socket(ip, port);
		hndConnection = new ConnectionHandle(new DataInputStream(connToServer
				.getInputStream()), new DataOutputStream(connToServer
				.getOutputStream()));
	}

	protected void disconnect() throws IOException {
		connToServer.close();
		hndConnection.close();
	}

	protected void send(Message msg) throws IOException {
		System.err.println("sent: " + msg + "\n");
		hndConnection.send(msg);
	}
	
	protected String receive () throws ParseException, SocketException {
		String msg = hndConnection.receive();
		System.err.println("received: " + msg + "\n");
		return msg;
	}
}